package com.edu.d1_ip;


import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 目标z;掌握InetAddress类的使用。
 */
public class Main {
    public static void main(String[] args) throws Exception {

        /**
         * 获取本机IP地址对象
         * LAPTOP-EKS9QNVS
         * 192.168.217.241
         */
        InetAddress ip = InetAddress.getLocalHost();
        InetAddress ipv4 = Inet4Address.getLocalHost();
        System.out.println(ip.getHostName());
        System.out.println(ip.getHostAddress());
        System.out.println(ipv4.getHostName());
        System.out.println(ipv4.getHostAddress());

        /**
         * 获取指定·的IP地址或者域名的IP地址对象
         * www.baidu.com
         * 110.242.68.3
         */
        InetAddress ip2 = InetAddress.getByName("www.baidu.com");
        System.out.println(ip2.getHostName());
        System.out.println(ip2.getHostAddress());

        /**
         *指的是，当前主机能否在6000毫秒内连通ip2的地址
         * 通俗的讲就是，主机能否在6000毫秒内ping同百度的地址
         * true
         */
        boolean reachable = ip2.isReachable(6000);
        System.out.println(reachable);
    }
}